package user

import (
	"db/models"
	"github.com/gorilla/mux"
	"net/http"
)

// MakeMuxer creates a http.Handler to manage all link operations. If
// a prefix is given, that prefix will be the first part of the
// url. This muxer will provide the following handlers and return a
// RESTful 404 on all others.
//
//  GET     prefix + /        Get all links.
//  POST    prefix + /        Create a new link.
//
//  GET     prefix + /{key}/  Get the link for the given key.
//  PUT     prefix + /{key}/  Update the link with the given key.
//  DELETE  prefix + /{key}/  Delete the link with the given key.
//
// See the functions related to these urls for more details on what
// each one does and the requirements behind it.

type Env struct {
	db     models.DataAccessObject
}

func MakeMuxer(prefix string) http.Handler {
	env := &Env{
		db:models.GetDBAdapter(),
	}
	var m *mux.Router

	// Pass through the prefix if we got one.
	if prefix == "" {
		m = mux.NewRouter()
	} else {
		m = mux.NewRouter().PathPrefix(prefix).Subrouter()
	}
	m.HandleFunc("/{id:[0-9]+}/info", env.UserInfo).Methods("GET")
	m.HandleFunc("/{id:[0-9]+}", env.User).Methods("GET")
	m.HandleFunc("/{id:[0-9]+}", env.UpdateUser).Methods("POST")
	m.HandleFunc("/{id:[0-9]+}", env.DeleteUser).Methods("DELETE")
	m.HandleFunc("", env.PutUser).Methods("PUT")
	m.Path("/all").Methods("GET").HandlerFunc(env.UserAll)//.Queries("limit","{[0-9]*?}","start","{[0-9]*?}")
	return m
}